//
//  SEGSideslipView.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/9.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGSideslipView.h"
#define kSEGViewHeight 44.0
#define kSEGViewWidth LYScreenWidth() - 30

@implementation SEGDoorImageView{
    CGFloat X;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    X = [touches.anyObject locationInView:self.superview].x - [touches.anyObject previousLocationInView:self.superview].x;
    self.left += X;
    if (self.delegate && [self.delegate respondsToSelector:@selector(seg_canOpenDoor:location: Event:)]) {
        [self.delegate seg_canOpenDoor:NO location:self.frame.origin Event:@"touchesBegan"];
    }
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGFloat maxWidth = self.superview.width - self.padding - self.width;
    if(self.left >= maxWidth){
        self.left = maxWidth;
    }else{
        X = [touches.anyObject locationInView:self.superview].x - [touches.anyObject previousLocationInView:self.superview].x;
        self.left += X;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(seg_canOpenDoor:location: Event:)]) {
        [self.delegate seg_canOpenDoor:NO location:self.frame.origin Event:@"touchesMoved"];
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGFloat maxWidth = self.superview.width - self.padding - self.width;
    BOOL canOpen = self.left >= maxWidth * 0.5 ? YES : NO;
    self.left = self.padding;
    if (self.delegate && [self.delegate respondsToSelector:@selector(seg_canOpenDoor:location: Event:)]) {
        [self.delegate seg_canOpenDoor:canOpen location:self.frame.origin Event:@"touchesEnded"];
    }
}
@end

@interface SEGSideslipView()

@property (nonatomic, strong)UIImageView *backImageView;
@property (nonatomic, strong)UILabel *nameLabel;
@end

@implementation SEGSideslipView
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.height = kSEGViewHeight;
        frame.size.width = kSEGViewWidth;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = kColor_W;
    self.exclusiveTouch = YES;
    self.layer.cornerRadius = kSEGViewHeight * 0.5;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = kColor_L2.CGColor;
    self.layer.borderWidth = 1;
    
    [self addSubview:self.nameLabel];
    [self addSubview:self.backImageView];
    [self addSubview:self.lockImageView];
    
    self.lockImageView.hidden = NO;
    CGFloat padding = (self.height - self.lockImageView.height) * 0.5;
    self.lockImageView.frame = CGRectMake(padding, padding, self.lockImageView.width, self.lockImageView.height);
    self.lockImageView.padding = padding;
    self.lockImageView.layer.cornerRadius = self.lockImageView.width * 0.5;
    self.lockImageView.layer.masksToBounds = YES;
    self.lockImageView.layer.borderWidth = 1.5;
    self.lockImageView.layer.borderColor = kColor_W.CGColor;
    
    self.backImageView.frame = CGRectMake(padding, padding, self.lockImageView.width, self.lockImageView.height);
    self.backImageView.layer.cornerRadius = self.lockImageView.width * 0.5;
    self.backImageView.layer.masksToBounds = YES;
    
    self.nameLabel.hidden = NO;
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.lockImageView.frame) + 10, 0, self.lockImageView.width, self.lockImageView.height);
    self.nameLabel.left = CGRectGetMaxX(self.lockImageView.frame) + 10;
    self.nameLabel.top = 0;
    self.nameLabel.width = self.width - CGRectGetMinX(self.nameLabel.frame) - 15;
    self.nameLabel.height = self.height;
    return self;
}

-(void)seg_canOpenDoor:(BOOL)canOpen location:(CGPoint)location Event:(NSString *)event{
    self.backImageView.width = CGRectGetMaxX(self.lockImageView.frame) - (self.height - self.lockImageView.height) * 0.5;
    if (self.delegate && [self.delegate respondsToSelector:@selector(seg_canOpenDoor:Event:)]) {
        [self.delegate seg_canOpenDoor:canOpen Event:event];
    }
}

-(SEGDoorImageView *)lockImageView{
    if (!_lockImageView) {
        _lockImageView = [[SEGDoorImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_opendoor_key")]];
        [_lockImageView setUserInteractionEnabled:YES];
        _lockImageView.hidden = YES;
        _lockImageView.delegate = self;
    }
    return _lockImageView;
}

-(UIImageView *)backImageView{
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc]init];
        _backImageView.backgroundColor = kColor_T;
    }
    return _backImageView;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kColor_1;
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _lockImageView.hidden = NO;
    }
    return _nameLabel;
}

-(void)setDoorName:(NSString *)doorName{
    _doorName = doorName;
    self.nameLabel.text = _doorName;
}
@end
